
"""
class TestBlockCache(unittest.TestCase):
    def test_main(self):
        bcache = BlockCache()
        bcache.get_block(dev, 0, 512)
        bcache.alloc_block()
        bcache.destroy_block()


class Mapping(ABC):
    def __init__(self, size: int, dev: Device, start: int, end: int, bcache: BlockCache):
        if size == end - start:
            self.__lstart = 0
            self.__lend = size

            self.__start = start
            self.__end = end

            self.__dev = dev
            self.__bcache = bcache
        else:
            raise ValueError(f"mapping invalid, size:{size} !=  end:{end} -start:{start}")

    def is_in_logical(self, index: int) -> bool:
        pass
    def logical_index(self, physical_index: int) -> int:
        pass
    def is_in_physical(self, index: int) -> bool:
        pass
    def physical_index(self, logical_index: int) -> int:
        pass


class TestMapping(unittest.TestCase):
    def test_mapping(self):
        mapping = Mapping(10, None, 10, 20))

        sector_0 = mapping.block(0, 0, 512)
        sector_1 = mapping.block(1, 0, 512)

        
    def test_main(self):

        fat12MBR = Mapping(1, dev, (0, 1))

        bcache = BlockCache()
        mbr = fat12MBR(dev, bcache)

        reader = ByteReader(mbr.block(0, 0, 512))
        mbr.total_sector = reader.readUint32()
        mbr.xx = reader.readUint16()

        mbr.start_sector()
        mbr.end_sector()
        mbr.nsectors()
        mbr.total_sector()


        fat = fat12Fat(dev, bcache)
        fat.main().start_sector()
        fat.backup().start_sector()

        data = fat12Data(dev, bcache)
        root = fat12RootDirectory(dev, bache)
        for de in root.iter():
            print(de)
       
        data = fat12Data(dev, bcache)
        for s in data.iter():
            print(s)
                



#import fsim
class TestFsim(unittest.TestCase):
    def test_fs(self):
        file_system_type = FileSystemType("fat12")
        file_system = file_system_type.mount("a.img", "readable")

        # open_as_write, open_as_append
        f_regular = file_system.open_as_read("/a.txt")
        bs = f_regular.read(offset, size)
        print(bs.decode())
        f_regular.close()

        f_dir = file_system.open_as_read("/dir")
        bs = f_dir.read(offset, size)
        DirEntry.from_bytes(bs).print()
        f_dir.close()

        f_pipe = file_system.open_as_read("/stream_out")
        bs = f_pipe.read(offset, size)
        print(bs.decode())
        f_pipe.close()

        f_dev = file_system.open_as_read("/dev")
        bs = f_dev.read(offset, size)
        print(bs.decode())
        f_dev.close()

        try:
            f_regular = file_system.create("/a.txt", mode=664)
            f_regular.close()
        except e as FileExistError:
            print("Err: FileExist")

        try: 
            f_dir = file_system.mkdir("/abc/cde", mode=774)
            f_dir.close()
        except e as FileExistError:
            print("Err: FileExistError")

        try:
            f_dev = file_system.mknod("/dev/abc", mode=600)
            f_dev.close()
        except e as FileExistError:
            print("Err: FileExistError")

        file_system.delete("/a.txt")
        file_system.delete("/dir")
        file_system.delete("/stream_out")
        file_system.delete("/dev")

        f_dir = file_system.root()
        v = DirVisitor(f_dir)
        for e in v:
            print(e)

        file_system.unmount()
"""
